/*
 copyright 2020, Chen Wei <weichen302@gmail.com>
 version 0.0.3
Implement astronomical algorithms for finding solar terms and moon phases.

Truncated LEA-406 for calculate Moon's apparent longitude;

Reference:
    LEA-406: S. M. Kudryavtsev (2007) "Long-term harmonic development of
             lunar ephemeris", Astronomy and Astrophysics 471, 1069-1075
*/

#include <stdio.h>
#include <math.h>
#include "astro.h"

static double FRM[5] = {
    785939.924268, 1732564372.3047, -5.279, .006665, -5.522e-5
};

/*
 table for LEA-406 moon solution. Those terms are linear combination
 of integer multipliers of 14 variables (Arg_j_, j=1,14):
 Delaunay variables l, l', F, D;
 mean longitude of the ascending node of the Moon {Omega};
 mean longitudes of eight major planets {lambda}_pl_;
 and the general precession in longitude p_A_.
 terms of 3rd-degree and 4th-degree are ignored
 in arcsec
 average error Moon = 0.73", max 1.5"
*/
static double M_ARG[226][3] = {
    { 485868.249036, 1717915923.21779990, 31.87920 },
    { 1658653.158348, 1488007279.20020032, -44.62040 },
    { 2144521.407384, 3205923202.41800022, -12.74120 },
    { 971736.498072, 3435831846.43559980, 63.75840 },
    { 1287104.793048, 129596581.04809999, -0.55320 },
    { 671559.052464, 3479054525.69560003, -25.50240 },
    { 1172784.909312, -229908644.01759958, -76.49960 },
    { 371548.365300, 1358410698.15210032, -44.06720 },
    { 2630389.656420, 4923839125.63580036, 19.13800 },
    { 857416.614336, 3076326621.36990023, -12.18800 },
    { 801236.544012, -1588319342.16969991, -32.43240 },
    { 1072260.703692, 1602961601.20900011, -6.37060 },
    { 1772973.042084, 1847512504.26589990, 31.32600 },
    { 1472962.354920, -273131323.27759981, 12.76120 },
    {      0.000000,      0.00000000, 0.00000 },
    { 1157427.301500, 5196970448.91339970, 6.37680 },
    { -185690.803428, -1761138602.47780013, 57.38160 },
    { 3803174.565732, 4693930481.61820030, -57.36160 },
    { 1457604.747108, 5153747769.65339947, 95.63760 },
    { 3317306.316696, 2976014558.40040064, -89.24080 },
    { 2945757.951396, 1617603860.24830031, -45.17360 },
    { 3431626.200432, 3335519783.46610022, -13.29440 },
    { 586392.454656, -114954322.00879979, -38.24980 },
    { 2359365.496740, 1732558182.25710011, -6.92380 },
    { 1343284.863372, 4794242544.58769989, 19.69120 },
    { 3116257.905456, 6641755048.85359955, 51.01720 },
    { 2746972.885608, 477658.07790603, -67.34420 },
    { 4289042.814768, 6411846404.83600044, -25.48240 },
    { 686916.660276, -1947824567.23539925, -108.37880 },
    { 315368.294976, -3306235265.38749981, -64.31160 },
    { 2330212.210812, 4967061804.89580059, -70.12280 },
    { -114319.883736, -359505225.06569958, -75.94640 },
    { 1558128.952728, 3320877524.42679977, 25.50860 },
    { -429688.178712, 2946730040.32180023, -11.63480 },
    { 2258841.291120, 3565428427.48369980, 63.20520 },
    { 2574209.586096, 259193162.09619999, -1.10640 },
    { -915556.427748, 1228814117.10400033, -43.51400 },
    { 450160.398036, -6962890.54310000, 7.47222 },
    { 1958830.603956, 1444784599.94020033, 44.64040 },
    { 2816080.459848, 6684977728.11359978, -38.24360 },
    { 2516069.772684, 4564333900.57010078, -56.80840 },
    { 1643295.550536, 6914886372.13119984, 38.25600 },
    { 2730913.862040, 3090968880.40920067, -50.99100 },
    { 3917494.449468, 5053435706.68389988, 18.58480 },
    { 2030201.523648, 2846417977.35230112, -88.68760 },
    { 2088341.337060, -1458722761.12159991, -32.98560 },
    { 4232862.744444, 1747200441.29640031, -45.72680 },
    { 2459889.702360, -100312062.96949959, -77.05280 },
    { 185857.561872, -402727904.32569981, 13.31440 },
    { 4774911.063804, 8129762328.05380058, 6.39680 },
    { 1943472.996144, 6871663692.87119961, 127.51680 },
    { 3001938.021720, 6282249823.78790092, -24.92920 },
    { 100524.205620, -1832870245.22659969, -70.12900 },
    { 2760067.147968, -143534742.22949982, 12.20800 },
    { 300177.445608, -43222679.26000023, 89.26080 },
    { 2845233.745776, 3450474105.47490025, 24.95540 },
    { 2245045.613004, 1373052957.19140100, -82.87020 },
    { 2831438.067660, 1258098635.18260098, -121.12000 },
    { 1829153.112408, 6512158467.80550003, 51.57040 },
    { 3060077.835132, 1977109085.31399989, 30.77280 },
    { 2134680.712596, 1725351443.01107621, -44.04921 },
    { 1873497.247704, 14642259.03930020, -38.80300 },
    { 3602126.154492, 8359670972.07139969, 82.89640 },
    { 3301948.708884, 8402893651.33139992, -6.36440 },
    { 201048.411240, -3665740490.45319939, -140.25800 },
    { 293448.038472, 81066394.15011901, 0.02638 },
    { 1643162.503716, 1717438685.61177826, -210.42628 },
    {  56180.070324, 4664645963.53960037, 20.24440 },
    { 3232841.134644, 1718393581.29570580, -35.46500 },
    { -170499.954060, -5024151188.60529900, -96.19080 },
    { 2538997.273764, 1731206461.65055728, -44.54126 },
    { 238013.777124, 118672081.90543799, 0.28560 },
    { 5090279.358780, 4823527062.66629982, -57.91480 },
    { 2043997.201764, 5038793447.64459991, 57.38780 },
    { 400701.651228, -1876092924.48659992, 19.13180 },
    { 5461827.724080, 6181937760.81840134, -101.98200 },
    { 501225.856848, -3708963169.71319962, -50.99720 },
    { -214844.089356, 1473365020.16090012, -5.81740 },
    { 2744709.540156, 5283344350.70149994, 95.08440 },
    { 1844343.961776, 3249145881.67800045, -102.00200 },
    { 936028.647072, 1710953032.67469978, 39.35142 },
    { -35707.851000, -1724878813.76090002, -24.40698 },
    { -600188.132772, -2077421148.28349924, -107.82560 },
    { 2444698.852992, 3162700523.15799952, 76.51960 },
    { 3963148.815276, 1714428507.27902675, -198.01200 },
    { 1043107.417764, 4837465223.84770012, -69.56960 },
    { 1343118.104928, 6958109051.39120007, -51.00480 },
    { 1958663.845512, 3608651106.74370003, -26.05560 },
    { 3216782.111076, 4808884803.62700081, -19.11180 },
    { 5947695.973116, 7899853684.03620148, -70.10280 },
    { 1528975.666800, 6555381147.06550026, -37.69040 },
    { 1121719.450500, 3472091635.15250015, -18.03018 },
    { 671725.810908, 1315188018.89209986, 45.19360 },
    { 4604411.109744, 3105611139.44850016, -89.79400 },
    { -9960733.695600, 135571.46521378, 63.51699 },
    { 1387628.998668, -1703273664.17849970, -70.68220 },
    {  26423.255340, -32537000.00799108, 44.93644 },
    { -1716792.971760, 2817133459.27370024, -11.08160 },
    { 2129163.799572, 8632802295.34899902, 70.13520 },
    { 1578859.855344, 8210672.20779327, -214.97146 },
    { 1228964.979636, 4434737319.52200031, -56.25520 },
    { 2287389.559392, 162132438.11736214, -200.33773 },
    { 129677.491548, -5067373867.86530018, -6.93000 },
    { 3131615.513268, 1214875955.92260027, -31.85920 },
    { -1401424.676784, -489101806.11379957, -75.39320 },
    { 4975959.475044, 4464021837.60060215, -133.86120 },
    { 4403362.698504, 6771351629.90170002, 50.46400 },
    { 5576147.607816, 6541442985.88409996, -26.03560 },
    { 3487806.270756, 8000165747.00570107, 6.95000 },
    { 4018018.655088, 3220565461.45730019, -51.54420 },
    { 3477280.566240, -3487415.93877248, -229.89120 },
    { 2444532.094548, 5326567029.96150017, 5.82360 },
    { 1743819.756156, 5082016126.90460014, -31.87300 },
    { 2545223.058612, 1329830277.93140078, 6.39060 },
    { 3746994.495408, 29284518.07860041, -77.60600 },
    { 1648812.463560, 7435519.79327631, -75.92841 },
    { -2202661.220796, 1099217536.05590034, -42.96080 },
    { 6176636.963928, 606718.56467720, 146.34632 },
    { 1443809.068992, 2961372299.36110115, -50.43780 },
    { 5260779.312840, 9847678251.27160072, 38.27600 },
    { 1420639.381224, 1369335197.29476237, -44.90600 },
    { 4474733.618196, 8172985007.31380081, -82.86400 },
    { 5299552.000908, 121384894.43375297, 62.45638 },
    { 1602473.088024, -3176638684.33939981, -64.86480 },
    { 476027.554248, 237344163.81087598, 0.57119 },
    { 4718730.993480, 3465116364.51420021, -13.84760 },
    {   9840.694788, 1480571759.40692401, 31.30801 },
    { 987094.105884, -1991047246.49539971, -19.11800 },
    { 1974021.453324, -1818227986.18729925, -108.93200 },
    { -2128477.768848, 113602752.48178008, 40.50488 },
    { 3988865.369160, 6455069084.09600067, -114.74320 },
    { 1906507.630260, 3087251120.51256227, -13.02680 },
    { -4107087.132120, 107747819.74584675, 210.09281 },
    { 863813.842884, 1487527316.94823694, -132.65705 },
    { 628736.401044, -1325874841.08283782, -155.71733 },
    { 602451.478224, -3205445544.34009504, -54.60300 },
    { 2939360.722848, 3206403164.66996431, 75.29545 },
    { 743096.730600, 2716821396.30420065, -88.13440 },
    { -2587922.762544, -1636850170.74401093, 53.56212 },
    { 1714833.228672, 6152653242.73980045, -24.37600 },
    { 2453492.473812, 1488487241.45216417, 43.41625 },
    { 4503886.904124, 4938481384.67510033, -19.66500 },
    { 247854.471912, 1599243841.31236196, 31.59360 },
    { -700712.338392, -244550903.05689979, -37.69660 },
    { -41775.577452, -1650139296.84567857, -64.69155 },
    { 142868.152008, -3043790764.30063820, -187.59653 },
    { 824406.231780, 3722788.71663815, -36.85218 },
    { 3760707.671856, 1406941526.72641158, -130.06172 },
    { -385344.043416, -3550786168.44439936, -102.00820 },
    { -1616186.264472, 1798981675.69158912, 117.32052 },
    { 6433564.222152, 9617769607.25400162, -38.22360 },
    { -394475.866380, 1474716740.76744270, 31.80006 },
    { 3787816.957920, 10120809574.54920006, 25.51480 },
    { 271024.159680, 3191280943.37870026, 26.06180 },
    { -3915532.327356, -1718055041.34457111, -247.79376 },
    { -2943795.829284, 1717776805.09102821, -184.03536 },
    { 4588422.072480, 1333648.70949970, 213.90556 },
    { -327467.049516, -1015133.54049925, 95.02473 },
    { 2429341.245180, 8589579616.08899975, 159.39600 },
    { 723882.026160, 1836588005.12323785, 32.16480 },
    { 3861314.379144, 388789743.14429998, -1.65960 },
    { 1544333.274612, 1128502054.13450146, -120.56680 },
    { 4246575.920892, 3124857449.94421101, -98.18252 },
    { 4168107.377892, 3198959150.64730024, -5.80178 },
    { 1818627.407892, -1491494695.13897252, -185.27080 },
    { 2315021.361444, 8230074391.02330017, 83.44960 },
    { 3331101.994812, 5168390028.69269943, 56.83460 },
    {  15357.607812, -5426879092.93099976, -82.87640 },
    { 10479231.070128, -16418126.94190380, 74.77548 },
    { 1828986.353964, 8676024974.60899925, -19.12560 },
    { 4174722.931032, 6052341179.77030182, -101.42880 },
    { 2577500.752860, -1199777.09773506, -217.20541 },
    { 1182625.604100, 1250663115.38932443, -45.19159 },
    { 123665.467464, 10925660.37799100, -0.30604 },
    { 4960601.867232, 9890900930.53160095, -50.98480 },
    { 3245935.397004, 1574381180.98829985, 44.08720 },
    { 1668493.853136, 2968579038.60712433, -13.31239 },
    { 957940.819956, 1243456376.14330149, -82.31700 },
    { 6461919.320184, 15996023.02879967, 179.55064 },
    { 1101413.989620, -1631542021.42970014, 56.82840 },
    { -377031.566700, -1812274039.13709879, -19.73467 },
    { 1472795.596476, 1890735183.52590013, -57.93480 },
    { -857249.855892, -5240193128.17339993, 82.88400 },
    { 4087994.403528, 10077586895.28919983, 114.77560 },
    {  11070.933228, -1715208268.04166102, 26.29335 },
    { 801403.302456, -3752185848.97319984, 38.26360 },
    { 615545.740584, -3349457944.64750004, 24.94920 },
    { 2014843.915836, 8273297070.28330040, -5.81120 },
    { -2546147.185092, 13289126.10166723, 118.25367 },
    { 4660591.180068, 7770257102.98810196, -69.54960 },
    { 2620548.961632, 3443267366.22887611, -12.17001 },
    { -284819.837796, -5383656413.67099953, -172.13720 },
    { 3008350.635300, -1716566657.86483884, -189.51513 },
    { 3617317.003860, 5096658385.94390011, -70.67600 },
    { 4103352.011340, 4650707802.35820007, 31.89920 },
    { 120935.436876, 3212887254.18870020, -19.68062 },
    { 4103185.252896, 6814574309.16170025, -38.79680 },
    { 3545946.084168, 3695025008.53179979, 62.65200 },
    { 1929677.318028, 4679288222.57890129, -18.55860 },
    { 542048.319360, 6382561886.75740051, 52.12360 },
    { 3682239.128856, 1481043227.42950010, -37.68098 },
    { 1092991.606308, -1709705251.01000595, -246.85066 },
    { 2064728.104380, 1726126595.42559338, -183.09226 },
    { -1101247.231176, -532324485.37379980, 13.86760 },
    { 1310274.480816, 1721633683.11443806, -6.08500 },
    { 1685076.413688, 1455470279.19220901, 0.31604 },
    { -300010.687164, -2120643827.54349971, -18.56480 },
    { -3664055.126520, 2720871.13874409, -227.53801 },
    { 512291.504376, 1685378923.20980859, 76.81564 },
    { 3640898.842560, -1366622384.76644802, 107.07678 },
    { 417145.879260, -1477083922.99626732, -111.06649 },
    { 14236864.720236, -3208579.51268373, -163.76979 },
    { 4389567.020388, 4578976159.60939980, -95.61140 },
    { 786045.694644, 1674693243.95779943, 121.14000 },
    { 150612.259788, 1555781180.92637992, 76.83608 },
    { 459444.993696, 1750452923.22579098, -13.05724 },
    { 176897.182608, 3435351884.18363619, -24.27825 },
    { -3529183.602852, -2591705.71692875, -226.73272 },
    { 169814.944332, -1498930214.93224931, -109.34239 },
    { -356190.757488, -6785289791.08309937, -38.80920 },
    { 3375446.130108, -1329126180.07349992, -33.53880 },
    { 2209276.773936, 1754174550.70710015, -50.44218 },
    {  55466.634672, -1606681665.27969599, -111.04605 },
    { 1131009.331860, -1880047940.86327744, -141.19115 },
    { 6062015.856852, 8259358909.10190010, 5.84360 },
    { 352362.086784, 226418503.43288499, 0.87723 },
};

/*
 table for LEA-406 moon solution
 Ak0    arcsec  Amplitude of the Fourier term
 Ak1    mas/yr  Amplitude of the 1st-order Poisson term
 Ak2    uas/yr2 Amplitude of the 2nd-order Poisson term
 { Ak0, Ak1, Ak2 } ...
*/
static double M_AMP[226][3] = {
    {  22639.5864251,  0.190648,  5.529914 },
    {   4586.4946082,  0.112352,  1.480719 },
    {   2369.9292836,  0.105206,  1.315910 },
    {    769.0251380,  0.013309,  0.373136 },
    {    666.4389699, 16.764899,  0.509567 },
    {    411.5952546,  0.002764,  0.176172 },
    {    211.6566179,  0.006615,  0.018535 },
    {    205.4430995,  5.163882,  0.191171 },
    {    191.9569463,  0.011624,  0.153634 },
    {    164.7319041,  4.138198,  0.167655 },
    {    147.3267606,  3.710258,  0.121729 },
    {    124.9935117,  0.001183,  0.034216 },
    {    109.3839297,  2.755639,  0.083615 },
    {     55.1780671,  0.002891,  0.006919 },
    {      0.0362449,  1.349731, 45.692267 },
    {     45.0995619,  0.000090,  0.030602 },
    {     39.5287366,  0.001225,  0.006971 },
    {     38.4307291,  0.004006,  0.032533 },
    {     36.1241571,  0.000898,  0.025549 },
    {     30.7733692,  0.002140,  0.019268 },
    {     28.3979625,  0.711689,  0.023432 },
    {     24.3591471,  0.612529,  0.022527 },
    {     18.5852985,  0.000437,  0.001521 },
    {     17.9562255,  0.451214,  0.014878 },
    {     14.5306503,  0.364691,  0.018609 },
    {     14.3797216,  0.001391,  0.015092 },
    {     14.2136673,  0.506954,  0.388974 },
    {     13.8992155,  0.002252,  0.015581 },
    {     13.1942141,  0.000494,  0.002232 },
    {      9.6793682,  0.243746,  0.009212 },
    {      9.3659379,  0.000333,  0.007150 },
    {      8.6056849,  0.216461,  0.006770 },
    {      8.4533524,  0.000386,  0.004712 },
    {      8.0506614,  0.404649,  0.009300 },
    {      7.6304201,  0.192240,  0.006663 },
    {      7.4493429,  0.375004,  0.006549 },
    {      7.3717333,  0.370557,  0.007936 },
    {      7.0921386,  0.076778,  0.005079 },
    {      6.3823525,  0.000616,  0.002913 },
    {      5.7416948,  0.000257,  0.005899 },
    {      4.3741497,  0.109740,  0.005590 },
    {      3.9976117,  0.000026,  0.003764 },
    {      3.2097860,  0.000695,  0.001883 },
    {      2.9146498,  0.073322,  0.003241 },
    {      2.7319853,  0.068620,  0.003016 },
    {      2.5682050,  0.129294,  0.002534 },
    {      2.5220218,  0.127230,  0.002163 },
    {      2.4889981,  0.063117,  0.001806 },
    {      2.1461452,  0.053905,  0.001745 },
    {      1.9777716,  0.000440,  0.002642 },
    {      1.9336777,  0.000069,  0.001990 },
    {      1.8708161,  0.046884,  0.002864 },
    {      1.7530780,  0.000160,  0.000246 },
    {      1.4372113,  0.036159,  0.001127 },
    {      1.3713028,  0.000041,  0.000141 },
    {      1.2619335,  0.031743,  0.001156 },
    {      1.2241492,  0.000087,  0.000559 },
    {      1.1868343,  0.000109,  0.000580 },
    {      1.1770227,  0.029544,  0.001701 },
    {      1.1619378,  0.058532,  0.001032 },
    {      1.1431465,  0.001145,  0.004116 },
    {      1.0779824,  0.027072,  0.000890 },
    {      1.0595012,  0.000125,  0.001494 },
    {      0.9902267,  0.000067,  0.001314 },
    {      0.9482810,  0.000049,  0.000381 },
    {      0.8216250,  0.000304,  0.000090 },
    {      0.7791738,  0.055305,  0.066699 },
    {      0.7517241,  0.037784,  0.001034 },
    {      0.7322303,  0.044974,  0.051647 },
    {      0.6694015,  0.016850,  0.000770 },
    {      0.6436261,  0.000332,  0.000422 },
    {      0.6388982,  0.000303,  0.000313 },
    {      0.6352378,  0.016019,  0.000773 },
    {      0.5840142,  0.000053,  0.000496 },
    {      0.5833144,  0.000000,  0.000051 },
    {      0.5715783,  0.000117,  0.000766 },
    {      0.5606404,  0.000000,  0.000176 },
    {      0.5568122,  0.014135,  0.000470 },
    {      0.5459375,  0.013761,  0.000538 },
    {      0.5356650,  0.000028,  0.000298 },
    {      0.5025507,  0.011924,  0.000650 },
    {      0.5005117,  0.011841,  0.000959 },
    {      0.4784073,  0.012033,  0.000371 },
    {      0.4537245,  0.000048,  0.000336 },
    {      0.4381375,  0.054206,  0.022773 },
    {      0.4262242,  0.010710,  0.000506 },
    {      0.4203282,  0.000000,  0.000355 },
    {      0.4134074,  0.010391,  0.000347 },
    {      0.4041956,  0.000444,  0.000404 },
    {      0.3945214,  0.000105,  0.000652 },
    {      0.3821338,  0.009597,  0.000535 },
    {      0.3748102,  0.011708,  0.000597 },
    {      0.3744777,  0.009395,  0.000354 },
    {      0.3575903,  0.009033,  0.000359 },
    {      0.3416863,  0.352069,  0.220915 },
    {      0.3495542,  0.008691,  0.000624 },
    {      0.3435465,  0.009341,  0.007387 },
    {      0.3398415,  0.025631,  0.000333 },
    {      0.3286524,  0.000000,  0.000403 },
    {      0.3248895,  0.001569,  0.031518 },
    {      0.3087352,  0.015508,  0.000464 },
    {      0.3016226,  0.005046,  0.028777 },
    {      0.3015612,  0.007593,  0.000321 },
    {      0.3008704,  0.000080,  0.000190 },
    {      0.2942052,  0.014800,  0.000232 },
    {      0.2925609,  0.000043,  0.000351 },
    {      0.2902265,  0.007303,  0.000385 },
    {      0.2891080,  0.007270,  0.000414 },
    {      0.2825095,  0.007069,  0.000521 },
    {      0.2737910,  0.006879,  0.000263 },
    {      0.2736062,  0.038332,  0.094167 },
    {      0.2633701,  0.006632,  0.000249 },
    {      0.2542937,  0.000000,  0.000192 },
    {      0.2530370,  0.000035,  0.000148 },
    {      0.2500111,  0.012673,  0.000241 },
    {      0.2494213,  0.007693,  0.009143 },
    {      0.2469758,  0.018629,  0.000176 },
    {      0.1180438,  0.242859,  0.232513 },
    {      0.2314117,  0.005794,  0.000255 },
    {      0.2185403,  0.000055,  0.000408 },
    {      0.2111003,  0.000156,  0.000174 },
    {      0.2013358,  0.000026,  0.000300 },
    {      0.1944693,  0.003998,  0.005565 },
    {      0.1931190,  0.009721,  0.000207 },
    {      0.1844919,  0.000278,  0.000754 },
    {      0.1835925,  0.008827,  0.000266 },
    {      0.1825829,  0.000095,  0.000340 },
    {      0.1789891,  0.000029,  0.000060 },
    {      0.1762440,  0.004467,  0.000147 },
    {      0.1751178,  0.006068,  0.003488 },
    {      0.1697756,  0.000016,  0.000207 },
    {      0.1649126,  0.000149,  0.000035 },
    {      0.1643790,  0.002513,  0.016571 },
    {      0.1609152,  0.046251,  0.052193 },
    {      0.1608887,  0.002702,  0.015539 },
    {      0.1603243,  0.024905,  0.034943 },
    {      0.1590996,  0.028613,  0.024866 },
    {      0.1578112,  0.007932,  0.000203 },
    {      0.1537976,  0.004754,  0.006421 },
    {      0.1522670,  0.007645,  0.000263 },
    {      0.1514863,  0.048990,  0.060578 },
    {      0.1499154,  0.003761,  0.000172 },
    {      0.1432907,  0.000164,  0.002489 },
    {      0.1363584,  0.003428,  0.000102 },
    {      0.1360093,  0.005055,  0.007270 },
    {      0.1342991,  0.002202,  0.012985 },
    {      0.1317702,  0.012996,  0.046203 },
    {      0.1312510,  0.004067,  0.005441 },
    {      0.1281174,  0.000034,  0.000093 },
    {      0.1274662,  0.003938,  0.005376 },
    {      0.1261665,  0.000044,  0.000252 },
    {      0.1251903,  0.000032,  0.000034 },
    {      0.1238568,  0.000012,  0.000201 },
    {      0.1207260,  0.003037,  0.000112 },
    {      0.1177733,  0.104057,  0.092375 },
    {      0.1166401,  0.105848,  0.086210 },
    {      0.1145920,  0.065094,  0.062264 },
    {      0.1127967,  0.017578,  0.045202 },
    {      0.1110004,  0.000000,  0.000148 },
    {      0.1100790,  0.000037,  0.000050 },
    {      0.1014638,  0.007699,  0.000024 },
    {      0.0998164,  0.002513,  0.000094 },
    {      0.0992925,  0.003076,  0.004114 },
    {      0.0971982,  0.005531,  0.000173 },
    {      0.0935175,  0.010642,  0.005973 },
    {      0.0932035,  0.002337,  0.000160 },
    {      0.0920486,  0.002316,  0.000087 },
    {      0.0915362,  0.000000,  0.000057 },
    {      0.0909223,  0.014292,  0.002950 },
    {      0.0909178,  0.000000,  0.000101 },
    {      0.0903302,  0.002261,  0.000156 },
    {      0.0585008,  0.089697,  0.078271 },
    {      0.0891047,  0.000146,  0.000132 },
    {      0.0860207,  0.003064,  0.003710 },
    {      0.0849998,  0.000015,  0.000149 },
    {      0.0847363,  0.002131,  0.000072 },
    {      0.0840293,  0.000147,  0.000081 },
    {      0.0831051,  0.002088,  0.000051 },
    {      0.0829235,  0.005841,  0.006803 },
    {      0.0828709,  0.002083,  0.000063 },
    {      0.0824031,  0.004719,  0.002548 },
    {      0.0805004,  0.002028,  0.000058 },
    {      0.0801760,  0.000019,  0.000000 },
    {      0.0776552,  0.000012,  0.000133 },
    {      0.0755097,  0.005767,  0.017169 },
    {      0.0752280,  0.000083,  0.000049 },
    {      0.0750004,  0.001874,  0.000066 },
    {      0.0737291,  0.001851,  0.000119 },
    {      0.0718922,  0.005260,  0.006839 },
    {      0.0714181,  0.001784,  0.000140 },
    {      0.0686150,  0.000124,  0.000210 },
    {      0.0684983,  0.000000,  0.000045 },
    {      0.0676977,  0.013106,  0.022804 },
    {      0.0674175,  0.001690,  0.000069 },
    {      0.0659954,  0.000011,  0.000077 },
    {      0.0657981,  0.003704,  0.000148 },
    {      0.0654081,  0.001645,  0.000080 },
    {      0.0651462,  0.003283,  0.000055 },
    {      0.0650640,  0.001653,  0.000084 },
    {      0.0643910,  0.003236,  0.000104 },
    {      0.0643077,  0.003626,  0.000118 },
    {      0.0640864,  0.000767,  0.007024 },
    {      0.0633090,  0.000680,  0.006279 },
    {      0.0631287,  0.003177,  0.000038 },
    {      0.0622945,  0.001764,  0.000987 },
    {      0.0617621,  0.001669,  0.001381 },
    {      0.0610645,  0.001547,  0.000186 },
    {      0.0610286,  0.034491,  0.021496 },
    {      0.0605601,  0.001685,  0.001334 },
    {      0.0590447,  0.001206,  0.001683 },
    {      0.0576070,  0.003983,  0.003844 },
    {      0.0574169,  0.012520,  0.037732 },
    {      0.0572562,  0.000022,  0.000070 },
    {      0.0567906,  0.000000,  0.000000 },
    {      0.0561596,  0.005382,  0.001305 },
    {      0.0559131,  0.001479,  0.001084 },
    {      0.0530823,  0.005285,  0.000393 },
    {      0.0527616,  0.030032,  0.013528 },
    {      0.0525526,  0.004791,  0.003973 },
    {      0.0516483,  0.001301,  0.000065 },
    {      0.0514262,  0.003899,  0.000000 },
    {      0.0513864,  0.003162,  0.000101 },
    {      0.0510076,  0.005738,  0.003400 },
    {      0.0507210,  0.001707,  0.002544 },
    {      0.0507028,  0.001274,  0.000084 },
    {      0.0506960,  0.005853,  0.006825 },
};

/*
 table for LEA-406 moon solution
 phik0  arcsec     Phase of the Fourier term
 phik1  arcsec     Phase of the 1st-order Poisson term
 phik2  arcsec     Phase of the 2nd-order Poisson term
 { phik0, phik1, phik2 } ...
*/
static double M_PHASE[226][3] = {
    {    0.000383901368,  -85.864631587374,  -90.051320519700 },
    {    0.002465002781,   43.853451435402,  -89.760808801732 },
    {    0.002701282720,    9.198419860616,  -89.927193714594 },
    {    0.000733167975,  -69.133923809631,  -89.854596360650 },
    {  179.998822844683,    0.033165685600,   -4.896329847950 },
    {  179.999821873246,  -92.231132704615,   90.039281835354 },
    {    0.002182942141,  115.589973490862,  -89.724570062688 },
    {    0.004492444068,  179.946598681046, -152.700235600240 },
    {    0.003074613545,    3.237177076528,  -89.045873461368 },
    {    0.004755315788,  179.925373896526, -139.458634581667 },
    {  179.998402135869,    0.029599610744,  -21.201860289276 },
    { -179.998700951455,  -21.042848422574,   85.696683781596 },
    {  179.998830243184,    0.038692795619,   12.231960211961 },
    {    0.002824379245,   -1.618145480441,  -93.712463437426 },
    {   90.000000000000,  -90.000000000000,  -90.000000000000 },
    { -179.999730775095,  136.948333197864,   90.252913650594 },
    {    0.000631501778,  -27.748331128161,   91.027469629070 },
    {    0.004768684748,   15.338642008082,  -91.448391559281 },
    {    0.000827624832,  -44.890207530000,  -94.464735632788 },
    {    0.004679600181,   28.129778606743,  -91.253712037737 },
    {  179.997410623704,   -0.012385396825,   19.711675596544 },
    { -179.999633530725,   -0.005264403413,   33.706044239720 },
    { -179.998546923256,  -52.237586501500,   94.104800351413 },
    {   -0.001202833150, -179.943767466285, -161.488197277814 },
    {    0.006060440686,  179.946114884688, -128.325040292843 },
    {    0.003479681319,   -5.088467767454,  -88.879853308440 },
    {   76.222761080242,  131.248177879490,  158.937351156777 },
    {    0.005491991867,    5.559956085120,  -89.285634473759 },
    {    0.001670679988,  119.052640751395,   95.344742980084 },
    {  179.997512146320,    0.012469626353,  -38.622239081360 },
    { -179.997591940179, -130.753370044026,   90.132938283259 },
    {    0.004027243751,  179.894863506410, -167.685307438556 },
    { -179.997888374255, -164.568228146555,   94.313669562461 },
    {    0.004280309127,  179.942703717388, -140.168177475912 },
    {  179.999403180215,    0.057104471446,   29.127407493447 },
    { -179.986595625958,   -0.009533448592,   -7.291531705498 },
    {    0.004516315551,  179.946856948035, -147.990210285414 },
    {   -2.292723477875,  179.558678822974,   99.917394355623 },
    { -179.995813706528,  174.359278613627,   85.899138385237 },
    { -179.997028119590, -170.875555577117,   87.517830497342 },
    {    0.007586565777,  179.860744031295, -126.955336135073 },
    { -179.999173757590,  173.529013740000,   89.645385356640 },
    { -179.996333121479, -173.529824098564,   90.691511437192 },
    { -179.998440539648,    0.000498095926,   46.506659248843 },
    {    0.007351480495,  179.846498962627, -134.320841941915 },
    {  179.998955202477,    0.004715888323,  -24.817267057930 },
    { -179.974500785427,    0.037906703343,   16.516954102688 },
    {    0.004084722791,  179.949205415543, -177.339668887924 },
    {    0.005169265951,  179.947259672081, -162.782325495969 },
    {    0.005470571565,    7.802235215572,  -91.188350231592 },
    {    0.001979936864,  -49.093247644398,  -89.853610040000 },
    {    0.008430819626,  179.862846789852, -120.579155305642 },
    { -179.997429267858,    3.689592432343,   -8.984125112071 },
    {  179.999682180708,    0.034556477923,    0.880098853934 },
    { -179.998734104791,   23.889956013343,   92.315478473154 },
    {   -0.001089631169, -179.953391970037, -148.465693395845 },
    { -179.995711702960,  -44.744675254758,   91.037547248403 },
    {    0.005618410521,  141.891652358833,  -89.245762243791 },
    {    0.006176245341,  179.923324075525, -121.671975846595 },
    { -179.990710441378,   -0.006065350786,    4.820672054727 },
    { -179.850243125450,   10.554417037974,   75.954784760505 },
    {    0.004538058638,  179.991538999959, -178.668525637314 },
    {    0.004948942002,   -2.305853377327,  -89.055962190853 },
    { -179.996220310084, -162.841071063425,   90.115626432671 },
    {    0.001362391462,  124.153219100000,   89.375195054863 },
    { -179.987972335590,   -9.455484053495,   98.486655980000 },
    {  100.520850457896,   58.245822583006,  176.656756595668 },
    {    0.005558909986,  179.939402800171, -129.032487586412 },
    {   74.866340599937,  113.143692095032,  157.140609294154 },
    {  179.996802948398,    0.023597286958,  -49.820634167458 },
    {  179.991295527846,  105.627702205222,  104.722979510000 },
    {    1.229379639312,   14.658638231892,  174.161916553362 },
    { -179.997302559910,   -0.079892424376,   50.903582526549 },
    { -179.997054101519, -179.406547096258,   88.325519523174 },
    { -179.997923631717,  -21.306014230000, -111.306014230000 },
    {    0.008920446563,   16.507710861469,  -87.001504751198 },
    { -179.997496718333,  -49.229404680000,  -96.386960454652 },
    {  179.993029626649,    0.063953608450,   32.857661723889 },
    {  179.999568497340,    0.029889128486,   41.670863171014 },
    { -179.997678361057, -131.554420507361,   90.182772169547 },
    {   -6.998845124057, -179.836968150987,   95.494728149239 },
    {   -6.946998653071, -179.864579320978,   76.439431277611 },
    {    0.003950080511,  179.877986481689,  178.009646557383 },
    { -179.995214967737, -178.270591376070,   92.820667680820 },
    { -169.915008504888,  163.606608570999,  141.706947652864 },
    { -179.995168304093,   -0.129249467750,   51.249058012792 },
    {   -0.000588147800,   76.911637520000,  -89.818659700943 },
    {   -0.001541311372, -179.960228699023, -157.839192649548 },
    {    0.005446540202,  178.120205971300,  -91.077675513499 },
    {    0.009961559799,   12.474727024070,  -91.103784962745 },
    { -179.994627410236,   -0.125960849016,   58.152530864523 },
    {  -10.452602212154, -175.909739396951,   95.972435843391 },
    { -179.993551894629,   -0.075640934119,   40.075819425085 },
    { -179.999730936646,   -0.111110307792,   44.048546642547 },
    {  -77.540885610051, -152.586445555125,  101.407916729860 },
    {    0.012860009915,  179.181823683989,  -38.176426666159 },
    { -159.862611524727,    8.333796508938,  108.553241211148 },
    {    0.000894638748,  179.956445219452, -108.623909313499 },
    { -179.998497415727, -141.434388770000,   89.925582532280 },
    {   33.792050127818,  148.766561177540,  127.241668563970 },
    {    0.009118370918,  179.900634928826, -123.775932072104 },
    { -112.134619005092,  -20.559070831297,  -20.121839387134 },
    {   -0.002033212332, -179.939721200975,  132.145526586787 },
    {    0.006967986668,   11.254792408294,  -89.887013647003 },
    {    0.004247032092, -179.958621060283, -154.368735202973 },
    {    0.009442611117,   35.703533951771,  -89.937681650476 },
    { -179.997723052131,   -0.017116494452,   54.758385850839 },
    { -179.995947079637,   -0.068525925581,   57.217045528267 },
    {    0.009819658662,  179.838703270416, -112.860829392226 },
    {    0.001459400774,  179.958199617562, -139.757079434325 },
    { -167.517828096945,  168.948506773399,  132.172642029344 },
    {   -0.001169845673, -179.992214129080, -139.036692930000 },
    {    0.001621751548,  -34.394376710000,  -89.706775576900 },
    { -179.994253678680, -163.461615332369,   90.508788209221 },
    {  179.954707818255,    0.297463641368,   79.938876374848 },
    {   -0.288803957182,  138.401774201477,  -90.194124627437 },
    {   -0.000441652594,  179.983728062620, -113.490522358660 },
    {  113.159919693549,   21.205321636711,  -68.950861756151 },
    { -179.993471286898,   -0.138018683023,   48.941925280000 },
    {    0.008513287422,    9.383107446051,  -89.982476550838 },
    {  178.266197023425,  167.040019546567,   63.337092639167 },
    { -179.993411147698, -152.143374643636,   90.067296178865 },
    {  177.709033274638,  -94.278194341766,   86.467689692292 },
    {  179.998212230083,    0.011725483834,  -38.211204526568 },
    { -179.548460175794,   11.748806769316,  155.794165646379 },
    {  179.696182246530,    8.388992907636, -155.810091409325 },
    { -179.966541471463,  123.169858230588,  174.061712674745 },
    {    0.001277362909,  -87.646654344739,   28.795532201940 },
    {    0.006441557128,  179.856257333904,  171.660707410000 },
    {  -73.684186429799,   66.397350353519, -166.301305668267 },
    { -179.993990076579, -136.968231614654,   90.111372676863 },
    {  178.195523468679,  167.215437886350,  100.414547150000 },
    {   33.641453360113, -165.302705060093,  -60.601265278652 },
    {  -69.150402372136,  -59.249946857851, -144.807244596642 },
    { -112.036976032111,  -23.538862057265,  -19.703539149818 },
    {   71.225252170846,   89.953326699449,  149.522242168418 },
    { -106.906299285279, -128.752837957579,  -35.167482742815 },
    {    0.008709768323,  179.926987734485, -128.528021306430 },
    {  125.434998080106, -144.834917590238,   33.658347760855 },
    {    0.009988580138,  179.896870091933, -116.342563520000 },
    { -112.167984339780, -118.315481678004,  -31.324502874434 },
    {    0.001914516745,  179.958829517227, -128.259077640388 },
    {  178.919893127688,  111.151535580000,  127.496580553665 },
    {  179.997049090149,    0.013601633772,   -2.620698856245 },
    {  -72.167265771598, -161.189710332737,   19.930112286947 },
    { -111.985346564208,  -23.928553663926,  -20.132412817928 },
    {   29.213939211730,  160.784938812168,  -84.562976989473 },
    {   54.598102587697,  -34.751396609636,  146.388967571845 },
    {  179.997627140643,  -20.956099913023, -107.488668694704 },
    {  125.438781294500, -144.945781138477,   33.725620650288 },
    {    0.011052840008,   12.898827180000,  -89.856260770306 },
    {  179.874812111593, -138.253779548140,   27.781164827339 },
    { -179.995364958510, -152.171377200000,   90.034521688113 },
    {  179.999571716845,    0.088736680056,   43.415194343448 },
    {   15.485979928550,   79.333070815266,  171.905198795860 },
    {   15.814692146435,   81.813245835498,  173.563281819956 },
    {  131.151069984933, -154.395177439830,  -38.414622359212 },
    {  -44.655851925818,  117.757239601126,  162.851093530505 },
    {    0.002900038346,  135.182987450000,  -89.948686156302 },
    {    1.103187588210,   22.976396007451, -111.078340600000 },
    { -179.887622646628,   -0.016270483931,  -31.472745223488 },
    {    0.007797056081,  179.864439387698, -138.165985101854 },
    {   54.595537656712,  -34.659546623063,  146.299996591100 },
    {  -80.604321141847,   23.349211093147,  124.690639100328 },
    {    9.950565354210,  -20.646007710451,  -20.232712520916 },
    {    0.007228561268,  179.913696450803, -114.674710042095 },
    {    0.000031181430, -179.969483846305, -115.306109670000 },
    { -179.998385048108,   85.733997830000,  -94.266002170000 },
    {  126.785821299905,   46.152057795628,   40.582093829691 },
    {    0.000067308052,  -58.051764990000,  -90.033259843394 },
    {    0.011879132634,  179.748408306343, -114.695467227172 },
    { -170.230856074017,  100.037571146737,   -1.882374403492 },
    {   -0.844779089926,  176.042515812187, -118.079402338350 },
    {  -73.380351208713,  159.697723721737,  -39.602387509092 },
    { -179.992838936462, -160.881735064112,   90.089950323242 },
    {    0.008160895360,  179.745911260715, -149.577199957246 },
    {   -0.946094322429,  178.710437293623,  -77.509104380747 },
    { -179.993591655201,   -0.214491674788,   93.905327790000 },
    {  156.746173873619,  -64.861968042177,   49.372967047162 },
    { -179.999469648778,   -0.016408343945,  -35.948330450000 },
    {   37.928299901718,  129.004508305459,  132.215335260474 },
    {  179.998321758011,    0.020516890633,    4.395108701959 },
    { -179.991344977137,   26.519098943619,  -31.875040030000 },
    {    0.005727141458,    4.355481318395,  -90.149163090774 },
    {  -21.479998361998, -146.605834790916,   58.779750145468 },
    { -179.964083052415,  -55.875067383234,   47.387971540000 },
    {   -0.001625399610, -179.904773285909,  142.248731360116 },
    { -179.993620429096,   -0.123104591073,   70.321134490000 },
    { -162.315246851023,  -72.633139513377,  112.510164638260 },
    {    0.012390392562,  179.760835926114, -111.438742365499 },
    { -179.872748658637,   50.117769415320,  168.302109917160 },
    {    0.000466648467,  169.116621610000,   79.116621610000 },
    {   68.455744968383, -114.610536287778, -179.414411109053 },
    {   -0.002100097173,  179.947053742654, -135.363706761591 },
    { -179.991954083944, -149.820003150000,   89.975569316466 },
    { -102.019559358387,  161.184331716547,   85.320435106082 },
    {    0.000547819855,  179.949477302242, -126.016097727692 },
    { -179.987915461393,   -0.051829095371,    5.014976620001 },
    {    0.008347581988,  179.849649173293, -123.589509971061 },
    {    0.006800628510,  179.936140204095, -119.801617824531 },
    {  -73.109646466056,    9.949188683703,  119.696875238960 },
    {   38.312689959023, -174.350323749453,  129.275979873543 },
    {   38.530542970871, -178.317385228219,  132.123003310646 },
    {    0.006989175447, -179.665485193957, -124.587860108589 },
    {  177.277845106886,  126.415631259050, -109.793598391998 },
    { -161.442918729888,    2.240961856056,  106.127634621147 },
    { -179.990868095964,   -3.697578545908,    7.251882193224 },
    {   61.035930743232,  -40.663545438817, -108.996557846408 },
    { -158.629498383529,   16.851256552374,  110.695920982574 },
    {  176.824703041161,  -98.747691569943,   85.257139856799 },
    {  177.683627303808, -120.178114678752,  -84.678375180739 },
    {  145.813265306575,  165.236834102475, -115.686032935051 },
    { -179.990848592847, -174.767338145272,   87.971105506103 },
    {  179.998967305008, -128.346026290000, -128.346026290000 },
    {  109.706873518703, -161.692320544537,    8.071930386460 },
    {  -21.373532585359, -175.453622486338,   97.727650370240 },
    {  -76.964196141885,  -25.052062779649,  -88.184334546093 },
    {  174.044963900841,   69.697068381257, -103.438549174491 },
    {    1.411566523810,  -63.763692231235,   96.605146342888 },
    {   -0.002484367922, -179.922758430555,  124.122955805312 },
    { -179.975293067009,    0.144068585072, -127.623925030000 },
    {  -85.595401423341,   33.309416345973,   52.614419166008 },
    {   -2.682922479904,   90.346879348731,  101.825176323036 },
    {  -72.194222741362, -164.226260049548,   24.011727797009 },
    { -179.994957071833,   -0.078650599201,   64.442567813218 },
    { -167.705956663408, -121.678544311070,  135.639537931150 },
};

/*
 * LEA-406 Moon Solution
 *
 * Reference:
 *     Long-term harmonic development of lunar ephemeris.
 *         Kudryavtsev S.M.  <Astron. Astrophys. 471, 1069 (2007)>
 *
 */

/* compute moon ecliptic longitude using lea406 */
double lea406(double jd, int ignorenutation) {
    double t, tm, tm2;
    t = (jd - J2000) / 36525.0;
    tm = t / 10.0;
    tm2 = tm * tm;

    double V, arg;
    V = FRM[0] + (((FRM[4] * t + FRM[3]) * t + FRM[2]) * t + FRM[1]) * t;

    int i;
    for (i = 0; i < 226; i++) {
        arg = (M_ARG[i][0] + t * (M_ARG[i][1] + M_ARG[i][2] * t)) * ASEC2RAD;
        V +=    M_AMP[i][0] * sin(arg + M_PHASE[i][0] * DEG2RAD)
              + M_AMP[i][1] * sin(arg + M_PHASE[i][1] * DEG2RAD) * tm
              + M_AMP[i][2] * sin(arg + M_PHASE[i][2] * DEG2RAD) * tm2;
    }

    V *= ASEC2RAD;

    if (!ignorenutation) {
        V += nutation(jd);
        /* printf("debug lea406,  nutation been adjusted"); */
    }
    return V;
}

/* calculate the apparent position of the Moon, it is an alias to the
 * lea406 function
 */
double apparentmoon(double jd, int ignorenutation)
{
    return lea406(jd, ignorenutation);
}
